from django.shortcuts import render
from django.views import View
from goods.models import SKU
from collections import OrderedDict
from goods.models import GoodsChannel
from contents.models import ContentCategory
from contents.utils import get_categories


# Create your views here.


class ImageView(View):

    def get(self, request):
        skus = SKU.objects.all()
        skus_list = []
        for sku in skus:
            skus_list.append({
                'img_url': sku.default_image.url
            })
        return render(request, 'img.html', {'skus': skus_list})


class IndexView(View):
    def get(self, request):
        """
            渲染首页
        :param request:
        :return:
        """
        # 1、渲染分类导航数据
        categories = get_categories()
        # 2、渲染广告数据
        contents = {}
        # 查询所有广告分类
        contentcategorys = ContentCategory.objects.all()
        for contentcategory in contentcategorys:
            contents[contentcategory.key] = contentcategory.content_set.filter(status=True).order_by('sequence')
        data = {
            'contents': contents,
            'categories': categories
        }
        return render(request, 'index.html', data)
